// 山东德州扑克 - 代码生成素材示例
// 这些代码可以帮你在没有图片素材的情况下，用代码生成基本的游戏素材

import UIKit
import SwiftUI

// MARK: - 扑克牌生成器
class CardGenerator {
    
    // 生成扑克牌背面
    static func generateCardBack(size: CGSize = CGSize(width: 60, height: 84)) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        defer { UIGraphicsEndImageContext() }
        
        guard let context = UIGraphicsGetCurrentContext() else { return UIImage() }
        
        // 背景色 - 深绿色
        context.setFillColor(UIColor(red: 0.06, green: 0.32, blue: 0.20, alpha: 1.0).cgColor)
        context.fill(CGRect(origin: .zero, size: size))
        
        // 圆角矩形边框
        let borderRect = CGRect(x: 2, y: 2, width: size.width - 4, height: size.height - 4)
        let borderPath = UIBezierPath(roundedRect: borderRect, cornerRadius: 6)
        context.setStrokeColor(UIColor.black.cgColor)
        context.setLineWidth(2)
        context.addPath(borderPath.cgPath)
        context.strokePath()
        
        // 中央装饰图案
        let centerX = size.width / 2
        let centerY = size.height / 2
        let patternSize: CGFloat = 20
        
        context.setFillColor(UIColor.white.withAlphaComponent(0.3).cgColor)
        let patternRect = CGRect(x: centerX - patternSize/2, y: centerY - patternSize/2, 
                               width: patternSize, height: patternSize)
        context.fillEllipse(in: patternRect)
        
        return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
    }
    
    // 生成扑克牌正面
    static func generateCard(suit: String, rank: String, size: CGSize = CGSize(width: 60, height: 84)) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        defer { UIGraphicsEndImageContext() }
        
        guard let context = UIGraphicsGetCurrentContext() else { return UIImage() }
        
        // 白色背景
        context.setFillColor(UIColor.white.cgColor)
        context.fill(CGRect(origin: .zero, size: size))
        
        // 黑色边框
        let borderPath = UIBezierPath(roundedRect: CGRect(x: 1, y: 1, width: size.width - 2, height: size.height - 2), cornerRadius: 6)
        context.setStrokeColor(UIColor.black.cgColor)
        context.setLineWidth(1)
        context.addPath(borderPath.cgPath)
        context.strokePath()
        
        // 花色颜色
        let suitColor = (suit == "♥" || suit == "♦") ? UIColor.red : UIColor.black
        
        // 绘制左上角数字和花色
        let font = UIFont.systemFont(ofSize: 12, weight: .bold)
        let attributes: [NSAttributedString.Key: Any] = [
            .font: font,
            .foregroundColor: suitColor
        ]
        
        let rankText = NSAttributedString(string: rank, attributes: attributes)
        rankText.draw(at: CGPoint(x: 4, y: 4))
        
        let suitText = NSAttributedString(string: suit, attributes: attributes)
        suitText.draw(at: CGPoint(x: 4, y: 18))
        
        // 绘制中央花色
        let centerFont = UIFont.systemFont(ofSize: 24)
        let centerAttributes: [NSAttributedString.Key: Any] = [
            .font: centerFont,
            .foregroundColor: suitColor
        ]
        
        let centerSuit = NSAttributedString(string: suit, attributes: centerAttributes)
        let centerSize = centerSuit.size()
        let centerPoint = CGPoint(x: (size.width - centerSize.width) / 2, 
                                y: (size.height - centerSize.height) / 2)
        centerSuit.draw(at: centerPoint)
        
        return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
    }
}

// MARK: - 筹码生成器
class ChipGenerator {
    
    enum ChipType {
        case white, red, green, black, purple, gold
        
        var color: UIColor {
            switch self {
            case .white: return UIColor.white
            case .red: return UIColor.systemRed
            case .green: return UIColor.systemGreen
            case .black: return UIColor.black
            case .purple: return UIColor.systemPurple
            case .gold: return UIColor.systemYellow
            }
        }
        
        var value: String {
            switch self {
            case .white: return "1"
            case .red: return "5"
            case .green: